﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class ArticleDAO:IArticle
    {
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return SqlHelper.GetMaxID("AID", "T_Article");

        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int AID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Article");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string title)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Article");
            strSql.Append(" where Title=@Title");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.VarChar,200)
			};
            parameters[0].Value = title;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Models.ArticleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Article(");
            strSql.Append("Title,SubTitle,ComeFrom,Creator,CreateDate,Image,Summary,Content,CategoryCode,State,Url,TemplateID)");
            strSql.Append(" values (");
            strSql.Append("@Title,@SubTitle,@ComeFrom,@Creator,@CreateDate,@Image,@Summary,@Content,@CategoryCode,@State,@Url,@TemplateID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@SubTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@ComeFrom", SqlDbType.NVarChar,50),
					new SqlParameter("@Creator", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Image", SqlDbType.VarChar,250),
					new SqlParameter("@Summary", SqlDbType.NVarChar,2000),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@CategoryCode", SqlDbType.VarChar,12),
					new SqlParameter("@State", SqlDbType.VarChar,12),
					new SqlParameter("@Url", SqlDbType.VarChar,250),
					new SqlParameter("@TemplateID", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.SubTitle;
            parameters[2].Value = model.ComeFrom;
            parameters[3].Value = model.Creator;
            parameters[4].Value = model.CreateDate;
            parameters[5].Value = model.Image;
            parameters[6].Value = model.Summary;
            parameters[7].Value = model.Content;
            parameters[8].Value = model.CategoryCode;
            parameters[9].Value = model.State;
            parameters[10].Value = model.Url;
            parameters[11].Value = model.TemplateID;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Models.ArticleModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Article set ");
            strSql.Append("Title=@Title,");
            strSql.Append("SubTitle=@SubTitle,");
            strSql.Append("ComeFrom=@ComeFrom,");
            strSql.Append("Creator=@Creator,");
            strSql.Append("CreateDate=@CreateDate,");
            strSql.Append("Image=@Image,");
            strSql.Append("Summary=@Summary,");
            strSql.Append("Content=@Content,");
            strSql.Append("CategoryCode=@CategoryCode,");
            strSql.Append("State=@State,");
            strSql.Append("Url=@Url,");
            strSql.Append("TemplateID=@TemplateID,");
            strSql.Append("NewsID=@NewsID");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@SubTitle", SqlDbType.NVarChar,100),
					new SqlParameter("@ComeFrom", SqlDbType.NVarChar,50),
					new SqlParameter("@Creator", SqlDbType.NVarChar,50),
					new SqlParameter("@CreateDate", SqlDbType.DateTime),
					new SqlParameter("@Image", SqlDbType.VarChar,250),
					new SqlParameter("@Summary", SqlDbType.NVarChar,2000),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@CategoryCode", SqlDbType.VarChar,12),
					new SqlParameter("@State", SqlDbType.VarChar,12),
					new SqlParameter("@Url", SqlDbType.VarChar,250),
					new SqlParameter("@TemplateID", SqlDbType.Int,4),
                    new SqlParameter("@NewsID", SqlDbType.Int,4),
					new SqlParameter("@AID", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.SubTitle;
            parameters[2].Value = model.ComeFrom;
            parameters[3].Value = model.Creator;
            parameters[4].Value = model.CreateDate;
            parameters[5].Value = model.Image;
            parameters[6].Value = model.Summary;
            parameters[7].Value = model.Content;
            parameters[8].Value = model.CategoryCode;
            parameters[9].Value = model.State;
            parameters[10].Value = model.Url;
            parameters[11].Value = model.TemplateID;
            parameters[12].Value = model.NewsID;
            parameters[13].Value = model.AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Article ");
            strSql.Append(" where AID=@AID");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)
			};
            parameters[0].Value = AID;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string AIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Article ");
            strSql.Append(" where AID in (" + AIDlist + ")  ");
            int rows = SqlHelper.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.ArticleModel GetModel(int AID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Title,SubTitle,ComeFrom,Creator,CreateDate,Image,Summary,Content,CategoryCode,State,Url,TemplateID,NewsID from T_Article ");
            strSql.Append(" where AID=@AID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AID", SqlDbType.Int,4)			};
            parameters[0].Value = AID;

            Models.ArticleModel model = new Models.ArticleModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return new Models.ArticleModel();
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.ArticleModel ConvertToModel(DataRow row)
        {
            Models.ArticleModel model = new Models.ArticleModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["SubTitle"] != null)
                {
                    model.SubTitle = row["SubTitle"].ToString();
                }
                if (row["ComeFrom"] != null)
                {
                    model.ComeFrom = row["ComeFrom"].ToString();
                }
                if (row["Creator"] != null)
                {
                    model.Creator = row["Creator"].ToString();
                }
                if (row["CreateDate"] != null && row["CreateDate"].ToString() != "")
                {
                    model.CreateDate = DateTime.Parse(row["CreateDate"].ToString());
                }
                if (row["Image"] != null)
                {
                    model.Image = row["Image"].ToString();
                }
                if (row["Summary"] != null)
                {
                    model.Summary = row["Summary"].ToString();
                }
                if (row["Content"] != null)
                {
                    model.Content = row["Content"].ToString();
                }
                if (row["CategoryCode"] != null)
                {
                    model.CategoryCode = row["CategoryCode"].ToString();
                }
                if (row["State"] != null)
                {
                    model.State = row["State"].ToString();
                }
                if (row["Url"] != null)
                {
                    model.Url = row["Url"].ToString();
                }
                if (row["TemplateID"] != null && row["TemplateID"].ToString() != "")
                {
                    model.TemplateID = int.Parse(row["TemplateID"].ToString());
                }
                if (row["NewsID"] != null && row["NewsID"].ToString() != "")
                {
                    model.NewsID = int.Parse(row["NewsID"].ToString());
                }
            }
            return model;
        }

        protected List<Models.ArticleModel> ConvertToList(DataSet ds)
        {
            List<Models.ArticleModel> list = new List<Models.ArticleModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.ArticleModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Models.ArticleModel> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,Title,SubTitle,ComeFrom,Creator,CreateDate,Image,Summary,Content,CategoryCode,State,Url,TemplateID,NewsID ");
            strSql.Append(" FROM T_Article ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public List<Models.ArticleModel> GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" AID,Title,SubTitle,ComeFrom,Creator,CreateDate,Image,Summary,Content,CategoryCode,State,Url,TemplateID,NewsID ");
            strSql.Append(" FROM T_Article ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Article ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = SqlHelper.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public List<Models.ArticleModel> GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.AID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Article T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }

       
         /// <summary>
         /// sql 2000版分页存储过程
         /// </summary>
         /// <param name="PageSize"></param>
         /// <param name="PageIndex"></param>
         /// <param name="strWhere"></param>
         /// <returns></returns>
        public List<Models.ArticleModel> GetList(int PageSize, int PageIndex, string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "T_Article";
            parameters[1].Value = "AID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 1;  //非0值就是降序
            parameters[6].Value = strWhere;
            DataSet ds = SqlHelper.RunProcedure("UP_GetRecordByPage", parameters, "ds");
            return ConvertToList(ds);
        }
    }
}
